package org.example.lp.service.listener;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.example.lp.config.MQConstance;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
public class MqListener {

    @Autowired
    private ObjectMapper objectMapper;

    @RabbitListener(queues = MQConstance.DEMO_QUEUE)
    public void demoSend(String msg) {
        System.out.println("MQ 收到消息: " + msg);
        HashMap hashMap = null;
        try {
            hashMap = objectMapper.readValue(msg, HashMap.class);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        Long id = objectMapper.convertValue(hashMap.get("id"), Long.class);
        String username = (String) hashMap.get("username");
        System.out.println("id: " + id + ", username: " + username);
    }

}
